//
//  FDPublicServiceVC.m
//  FamousDoctorOnline
//
//  Created by 刘胤辰 on 16/7/7.
//  Copyright © 2016年 wszyxc. All rights reserved.
//
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS

#import "FDPublicServiceVC.h"
#import <Masonry.h>
#import "AppDelegate.h"

@interface FDPublicServiceVC ()<UIWebViewDelegate>

@property(nonatomic,strong)UIWebView *webview;
@property(nonatomic,strong)UIView *maskView;
@property(nonatomic,strong)UIActivityIndicatorView *indicator;

@end

@implementation FDPublicServiceVC

-(UIView *)maskView{
    
    if (_maskView==nil) {
        
        _maskView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        _maskView.alpha=0.6;
        _maskView.backgroundColor=[UIColor blackColor];
    }
    return _maskView;
}

-(UIWebView *)webview{
    
    if (_webview==nil) {
        
        _webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        [self.view addSubview:_webview];
    }
    return _webview;
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    
    //监测网络
    AppDelegate *appDelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    
    if (!appDelegate.isAchieve) {
        
        [self presentAlertVC:@"⚠️网络异常，请检查是否连接网络"];
    }
}

-(void)presentAlertVC:(NSString *)str{
    
    //创建网络异常弹框
    UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:str message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [self presentViewController:alertVC animated:YES completion:nil];
    
    UIAlertAction *identify=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }];

    UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertVC addAction:cancle];
    [alertVC addAction:identify];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *urlString = @"http://202.106.210.115:18080/hyde-pluto-h360/nt/appEntrance.htm";
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    self.webview.delegate=self;
    [self.webview loadRequest:request];
    [self.view addSubview:self.maskView];
    
    //创建小菊花
    self.indicator=[[UIActivityIndicatorView alloc]init];
    [self.maskView addSubview:self.indicator];
    //self.indicator.backgroundColor=[UIColor redColor];
    [self.indicator startAnimating];
    
    [self.indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        
                make.centerX.mas_offset(0);
                make.centerY.mas_offset(0);
                make.width.mas_equalTo(@50);
                make.height.mas_equalTo(@50);
    }];
    
    //创建菊花上的lab
    UILabel *label=[[UILabel alloc]init];
    [self.maskView addSubview:label];
    label.text=@"正在玩命加载中。。。。。";
    label.textColor=[UIColor whiteColor];
    label.font=[UIFont systemFontOfSize:14];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
                make.centerX.mas_offset(0);
                make.centerY.mas_offset(50);
    }];
    [label sizeToFit];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [self.indicator stopAnimating];
    [self.maskView removeFromSuperview];
    [self.indicator removeFromSuperview];
    NSLog(@"加载结束");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
